<?php
/* SVN FILE: $Id: post.php 253 2010-03-18 19:33:18Z miha.nahtigal $ */
/**
 * Short description for post.php
 *
 * Long description for post.php
 *
 * PHP versions 4 and 5
 *
 * Copyright (c) 2009, Miha Nahtigal
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright (c) 2009, Miha Nahtigal
 * @link          http://www.nahtigal.com/
 * @package       lil
 * @subpackage    lil.models
 * @since         v 1.0
 * @version       $Revision: 253 $
 * @modifiedby    $LastChangedBy: miha.nahtigal $
 * @lastmodified  $Date: 2010-03-18 19:33:18 +0000 (Thu, 18 Mar 2010) $
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Post class
 *
 * @uses          LilAppModel
 * @package       lil
 * @subpackage    lil.models
 */
class Post extends LilAppModel {
/**
 * name property
 *
 * @var string 'Post'
 * @access public
 */
	var $name = 'Post';
/**
 * actsAs property
 *
 * @var array
 * @access public
 */
	var $actsAs = array('Containable', 'Lil.LilSearch');
/**
 * belongsTo property
 *
 * @var array
 * @access public
 */
	var $belongsTo = array(
		'Blog' => array(
			'className'  => 'Lil.Blog',
			'foreignKey' => 'blog_id',
			'type'       => 'INNER'
		),
	);
/**
 * hasMany property
 *
 * @var array
 * @access public
 */
	var $hasMany = array(
		'Comment' => array(
			'className'  => 'Lil.Comment',
			'foreignKey' => 'post_id',
			'order'      => 'Comment.created'
		)
	);
/**
 * hasAndBelongsToMany property
 *
 * @var array
 * @access public
 */
	var $hasAndBelongsToMany = array(
		'Category' => array(
			'className'             => 'Lil.Category',
			'width'                 => 'Lil.CategoriesPost',
			// this has to be additional for custom configuration abilities
			'withClassName'         => 'CategoriesPost',
			'foreignKey'            => 'post_id',
			'associationForeignKey' => 'category_id',
		)
	);
/**
 * validate property
 *
 * @var array
 * @access public
 */
	var $validate = array(
		'title'	=> array(
			'rule'     => array('minLength', '1'),
			'required' => true
		),
		'slug' => array(
			'rule'       => 'checkSlug',
			'required'   => false,
			'allowEmpty' => true
		),
		'body' => array(
			'rule'     => array('minLength', '1'),
			'required' => true
		)
	);
/**
 * __construct method
 *
 * @param mixed $id
 * @param mixed $table
 * @param mixed $ds
 * @access private
 * @return void
 */
	function __construct($id = false, $table = null, $ds = null)	{
		$this->order = $this->name . '.created DESC';
		$this->belongsTo['Author'] = Configure::read('Lil.userTable');
		
		// create this on fly because table prefix cannot be set in HABTM
		$this->hasAndBelongsToMany['Category']['className'] = 'Lil.Category';
		if ($table_prefix = Configure::read('Lil.tablePrefix')) {
			$this->hasAndBelongsToMany['Category']['joinTable'] = $table_prefix.'categories_posts';
		}
		
		if (Configure::read('Lil.noCategories')) {
			unset($this->hasAndBelongsToMany['Category']);
		} else if ($category_assoc = Configure::read('Lil.categoryTable')) {
			$this->hasAndBelongsToMany['Category'] = $category_assoc;
		}
		
		if (Configure::read('Lil.noBlogs')) {
			unset($this->belongsTo['Blog']);
		}
		
		parent::__construct($id, $table, $ds);
	}
/**
 * checkSlug method
 *
 * @param array $data
 * @access public
 * @return boolean
 */
	function checkSlug($data) {
		if (Configure::read('Lil.slug') == 'auto') {
			return true;
		} else {
			return preg_match('/^[a-zA-Z0-9_-]+$/', $data['slug']);
		}
	}
/**
 * beforeSave callback
 *
 * @access public
 * @return boolean
 */
	function beforeSave() {
		if (
			empty($this->data['Post']['slug']) && 
			!empty($this->data['Post']['title']) && 
			!empty($this->data['Post']['blog_id'])
		) {
		
			$this->data['Post']['slug'] = strtolower(Inflector::slug($this->data['Post']['title'], '-'));
			$conditions = array(
				'Post.slug'    => $this->data['Post']['slug'],
				'Post.blog_id' => $this->data['Post']['blog_id'],
			);
			if (!empty($this->data['Post']['id'])) {
				$conditions['NOT'] = array('Post.id' => $this->data['Post']['id']);
			}
			
			// find non-existant slug
			if ($this->hasAny($conditions)) {
				$i = 1;
				do {
					$conditions['Post.slug'] = $this->data['Post']['slug'] . '-' . $i++;
				} while ($this->hasAny($conditions));
				
				$this->data['Post']['slug'] .= $conditions['Post.slug'];
			}
		}
		return true;
	}
}
?>